﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

using System.Data.Entity;
using DAO;

namespace BUS
{
    public class BUS_WRURGENT
    {
        WRMOEntities db = new WRMOEntities();

        public IList<WRURGENT> _GetList()
        {
            return db.WRURGENTs.ToList();
        }

        public IList<WRURGENT> _GetList(Func<WRURGENT, bool> cond)
        {
            return db.WRURGENTs.Where(cond).ToList();
        }

        public WRURGENT _GetObject(int ID)
        {
            return db.WRURGENTs.Where(p => p.ID == ID).FirstOrDefault();
        }

        public WRURGENT _GetObject(Func<WRURGENT, bool> cond)
        {
            return db.WRURGENTs.Where(cond).FirstOrDefault();
        }

        public WRURGENT _CreateObject()
        {
            return db.WRURGENTs.CreateObject();
        }

        public void _Save()
        {
            db.SaveChanges();
        }

        public void _Add(WRURGENT entity, bool save = true)
        {
            try
            {
                db.AddToWRURGENTs(entity);
                if (save)
                    _Save();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void _Update(WRURGENT entity)
        {
            try
            {
                WRURGENT _mainEntity = db.WRURGENTs.Where(p => p.ID == entity.ID).FirstOrDefault();
                if (_mainEntity != null)
                {
                    _mainEntity.ID = entity.ID;
                    _mainEntity.UrgentName = entity.UrgentName;
                    _mainEntity.UpdateDate = entity.UpdateDate;
                    _mainEntity.IDUpdateBy = entity.IDUpdateBy;
                    _mainEntity.DayLimit = entity.DayLimit;
                    _mainEntity.IDFuncPay = entity.IDFuncPay;
                    _mainEntity.Description = entity.Description;
                    _mainEntity.IDLang = entity.IDLang;
                    _mainEntity.Active = entity.Active;
                    _mainEntity.Keywords = entity.Keywords;
                }
                _Save();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void _Delete(int ID)
        {
            try
            {
                WRURGENT _mainEntity = db.WRURGENTs.Where(p => p.ID == ID).FirstOrDefault();
                if (_mainEntity != null)
                {
                    db.WRURGENTs.DeleteObject(_mainEntity);
                }
                _Save();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void _Delete(WRURGENT entity)
        {
            try
            {
                WRURGENT _mainEntity = db.WRURGENTs.Where(p => p.ID == entity.ID).FirstOrDefault();
                if (_mainEntity != null)
                {
                    db.WRURGENTs.DeleteObject(_mainEntity);
                }
                _Save();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public DataTable Get_Urgent_List()
        {
            IList<Get_Urgent_List_Result> query = db.Get_Urgent_List().ToList();
            return Common.Common.ConvertToDataTable(query.ToList());
        }
    }
}
